<?php
session_start();

/*
	status	|	Error
	1		|	Error in query execution
	2		|	Error in connection
	3		|	Error in database selection
	4		|	Some session variable is unavailable
*/
class wordData{
	public $id = '';
	public $wo = '';
	public $mean = '';
	public $sent = '';
	public $status = 0;
};

$data = new wordData();

if(session_id() != ''){
	if(!empty($_SESSION['username']) && !empty($_SESSION['password'])){
		
		$link = mysql_connect('localhost', $_SESSION['username'], $_SESSION['password']);
		if(!$link){
			$data->status = 2;
		}else{
			if(mysql_select_db('greprep',$link)){
				$res = mysql_query('CALL fetch_word(@word_id, @wo, @mean, @sent);', $link);
				
				if(!$res){
					$data->status = 1;
				}else{
					$sel = mysql_query('select @word_id, @wo, @mean, @sent;', $link);
					if(!$sel){
						$data->status = 1;
					}else{
						$val = mysql_fetch_array($sel);							
						$data->id = $val['@word_id'];
						$data->wo = $val['@wo'];			
						$data->mean = $val['@mean'];
						$data->sent = $val['@sent'];
						$data->status = 0;
						echo json_encode($data);
					}
				}
			}else{
				$data->status = 3;
			}
		}
	}else{
		$data->status = 4;
	}
}else{
	$data->status = 4;
}
?>
